import { message } from 'antd';
import {
    getWaitApprovallist,
    fetAgree,
    fetrefuse,
    fetreturn
} from '@/services/flowMine/waitApprovals';

export default {
    namespace: 'waitApproval',
    state: {
        waitApprovalList: [],
        waitApprovalPagination: {
            current: 1,
            pageSize: 10,
            total: 0
        },
        order: '',
        action: '',
        // 高级搜索字段
        title: '',
        startTime: '',
        endTime: '',
        templateNameList: [],
        status: ''
    },
    effects: {
        *fetchWaitApprovalList({ payload }, { call, put }) {
            const result = yield call(getWaitApprovallist, payload);
            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        // order: payload.orderBy,
                        waitApprovalList: result.data.rows,
                        waitApprovalPagination: {
                            pageSize: result.data.pageSize,
                            current: result.data.pageNo,
                            total: result.data.total
                        }
                    }
                });
            }
        },
        *fetchAgree({ payload, callBack }, { call, put, select }) {
            const Pagination = yield select(
                state => state.waitApproval.waitApprovalPagination
            );
            const result = yield call(fetAgree, payload);
            if (result) {
                if (result.code === 10000) {
                    message.success(result.msg);
                    callBack(true);
                    yield put({
                        type: 'save',
                        payload: {
                            waitApprovalPagination: {
                                ...Pagination,
                                current: 1
                            },
                            action: new Date().getTime()
                        }
                    });
                } else {
                    message.error(result.msg);
                    callBack(false);
                }
            }
        },
        *fetchRefuse({ payload, callBack }, { call, put, select }) {
            const Pagination = yield select(
                state => state.waitApproval.waitApprovalPagination
            );
            const result = yield call(fetrefuse, payload);
            if (result) {
                if (result.code === 10000) {
                    message.success('操作成功');
                    callBack(true);
                    yield put({
                        type: 'save',
                        payload: {
                            waitApprovalPagination: {
                                ...Pagination,
                                current: 1
                            },
                            action: new Date().getTime()
                        }
                    });
                } else {
                    message.error(result.msg);
                    callBack(false);
                }
            }
        },
        *fetchReturn({ payload, callBack }, { call, put, select }) {
            const Pagination = yield select(
                state => state.waitApproval.waitApprovalPagination
            );
            const result = yield call(fetreturn, payload);
            if (result) {
                if (result.code === 10000) {
                    message.success('操作成功');
                    callBack(true);
                    yield put({
                        type: 'save',
                        payload: {
                            waitApprovalPagination: {
                                ...Pagination,
                                current: 1
                            },
                            action: new Date().getTime()
                        }
                    });
                } else {
                    message.error(result.msg);
                    callBack(false);
                }
            }
        }
    },
    reducers: {
        save(state, { payload }) {
            return {
                ...state,
                ...payload
            };
        }
    }
};
